<?php

namespace app\agent\controller;

use app\common\model\Agent;
use app\common\model\AgentSales;
use app\common\model\LoginCode;
use app\common\model\LoginUsers;
use app\common\model\Merchant;
use app\common\model\AuthRole;
use app\common\model\DictItem;
use app\common\model\MerchantCashier;
use app\common\controller\AdminBase;
use think\Db;
use think\Session;
use thinkcms\auth\model\AuthRoleUser;


class Personnel extends AdminBase
{


    //业务员列表
    public function salesmanlist()
    {
        $request = request();
        $p_data = $request->get();
        $map = [];
        if ($p_data) {
            if ($p_data['xingming']) {
                $mar['username'] = array('like', '%' . trim($p_data['xingming']) . '%');
            } else {
                $p_data['xingming'] = '';
            }
        } else {
            $p_data['xingming'] = '';
        }
        $mar['agent'] = $this->get_agent_id();
        $sales=AgentSales::where($mar)->relation('LoginUsers')->select();
        $this->assign('p_data', $p_data);
        $this->assign('list', $sales);
        return $this->fetch();
    }

    //用户添加
    public function salesmanadd()
    {
        $request = request()->post();
        if (Request()->isPost()) {
            $level = 2002;
            $agent_id = $this->get_agent_id();
            $data['xingming'] = $request['xingming'];
            $data['tel'] = $request['tel'];
            $data['username'] = $request['username'];
            $data['password'] = MD5($request['password']);
            $data['level'] = $level;
            $data['Agent_sales'] = [
                'username'=>$request['xingming'],
                'sermch'=>0,
                'tel'=>$request['tel'],
                'agent'=>$agent_id
            ];
            $LoginUser = new LoginUsers();
            $sav = $LoginUser->together('Agent_sales')->allowField(true)->validate("LoginUsers.add")->save($data);
            if ($sav) {
                $sdata['role_id'] = 3;
                $sdata['user_id'] = $LoginUser->id;
                $AuthRoleUser = new AuthRoleUser();
                $AuthRoleUser->save($sdata);
                $this->success("添加成功",'personnel/salesmanlist');
            } else {
                $this->error($LoginUser->getError());
            }
        } else {
            $DictItem = new DictItem();
            $dice = $DictItem->where(['type_id' => 4])->select();
            $mab['id'] = array('in', $this->get_mer_mid());
            $Merchant = new Merchant();
            $mach = $Merchant->where($mab)->select();
            $this->assign('mach', $mach);
            $this->assign('dice', $dice);
            return $this->fetch();
        }
    }

    /*
     *
     * */
   public function per_machlist()
    {
        if (request()->isAjax()) {
            $map['id'] = array('in', $this->get_mer_mid());
            $Merchant = new Merchant();
            $list = $Merchant->where($map)->field('id,merchant')->select();
            foreach ($list as $k => $v) {
                $list[$k]['is_tz'] = $this->check_tz(request()->post('openid'), $v['id']);
            }
            return json($list);
        }
    }

    /*
     *
     * */
    private function check_tz($openid, $mid)
    {
        $mab['openid'] = $openid;

        $mab['mid'] = $mid;
        $Cashier = new MerchantCashier();
        $s = $Cashier->where($mab)->find();
        if ($s) {
            $data['is_tz'] = $s['is_tz'];
        } else {
            $data['is_tz'] = 0;
        }
        return $data;


    }
    //收银员修改
    public function salesmanedit()
    {
        $request = request()->post();

        if (Request()->isPost()) {
            $mae['id']=request()->post('id');
            $mae['agent']=$this->get_agent_id();
            $s=AgentSales::get($mae);
            $s->username=$request['xingming'];
            $s->tel=$request['tel'];
            $s->LoginUsers->xingming=$request['xingming'];
            $s->LoginUsers->tel=$request['tel'];
            if (!empty($request['password'])) {
                $s->LoginUsers->password=MD5($request['password']);
            }

            $sav =$s->together('LoginUsers')->save();
             if($sav!==false){
                 $this->success('修改成功','personnel/salesmanlist');
             }else{

                 $this->error('修改失败');
             }
        } else {
            $map['uid'] = request()->param('id');
            $machp['id'] = array('in', $this->get_mer_mid());
            $DictItem = new DictItem();
            $Merchant = new Merchant();
            $LoginUser = new LoginUsers();
            $mach = $Merchant->where($machp)->select();

            $dice = $DictItem->where('type_id', 4)->select();
            $AgentSales=new AgentSales();
            $mae['id']=request()->param('id');
            $mae['agent']=$this->get_agent_id();

            $loguser = $AgentSales->where($mae)->relation('LoginUsers')->find();


            $this->assign('mach', $mach);
            $this->assign('dice', $dice);
            $this->assign('loguser', $loguser);
            return $this->fetch();
        }
    }

    //员工删除
    public function salesmandel()
    {
        if (Request()->isAjax()) {
            $arr = request()->post();
            $AgentSales = new AgentSales();
            $LoginUser = $AgentSales::get($arr['id']);
            $res = $LoginUser->together('LoginUsers')->delete();
            if ($res) {
                $rarr = [
                    'code' => 0,
                    'mesg' => "删除成功！"
                ];
            } else {
                $rarr = [
                    'code' => 9001,
                    'mesg' => "删除失败！"
                ];
            }
            return json($rarr);
        }

    }
}
